using System.Collections;
using System.Collections.Generic;
using Main.CropPlant;
using Main.Details;
using Main.Inventory;
using UnityEngine;
using ZSW.Framework;

namespace Main.Item
{
    public class BaseItem : ZSWF_InitMonoBehaviour
    {
        public int ItemID = -1;
        public ItemDetails ItemDetails;
        private SpriteRenderer spriteRenderer;
        private BoxCollider2D boxCollider;

        public override void Init()
        {
            base.Init();
            spriteRenderer = GetComponentInChildren<SpriteRenderer>();
            boxCollider = GetComponent<BoxCollider2D>();
            if (ItemID != -1)
            {
                SetData(ItemID);
            }
        }

        public void SetData(int itemID)
        {
            this.ItemID = itemID;
            if (ZSWF_IOCContainer.Get<ItemManager>().TryGetItemDetails(itemID, out ItemDetails))
            {
                if (ItemDetails.ItemOnWorldSprite != null)
                {
                    spriteRenderer.sprite = ItemDetails.ItemOnWorldSprite;
                }
                else
                {
                    spriteRenderer.sprite = null;
                    LogError($"Item {ItemDetails.ItemID} {ItemDetails.ItemName} 没有设置ItemOnWorldSprite");
                }

                boxCollider.offset = new Vector2(0, spriteRenderer.sprite.bounds.center.y);
                boxCollider.size = new Vector2(spriteRenderer.sprite.bounds.size.x, spriteRenderer.sprite.bounds.size.y);

                if (ItemDetails.ItemType == ItemType.ReapableScenery)
                {
                    var ReapItem = gameObject.AddComponent<ReapItem>();
                    ReapItem.SetData(ItemID);

                    var interactive = gameObject.AddComponent<ItemInteractive>();
                }
            }
            else
            {
                LogError($"Item {itemID} 没有找到");
            }

        }
    }
}

